import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;

public class DoubleFieldRowUtil {
    

    public static class DoubleFieldItem {
        private int id;
        private String firstFieldContent;
        private String secondFieldContent;

        public DoubleFieldItem(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getFirstFieldContent() {
            return firstFieldContent;
        }

        public void setFirstFieldContent(String firstFieldContent) {
            this.firstFieldContent = firstFieldContent;
        }

        public String getSecondFieldContent() {
            return secondFieldContent;
        }

        public void setSecondFieldContent(String secondFieldContent) {
            this.secondFieldContent = secondFieldContent;
        }

    }

    public DoubleFieldRowUtil() {
    }

    public static List<DoubleFieldItem> getItems(Document document) {
        List<DoubleFieldItem> items = new ArrayList<DoubleFieldItem>();
        Element rootEl = document.getDocumentElement();
        NodeList rootChildNodes = rootEl.getChildNodes();
        int itemIndex = 0;
        for (int i = 0; i < rootChildNodes.getLength(); i++){
            Node rootChildNode = rootChildNodes.item(i);
            if (rootChildNode instanceof Text) continue;
            itemIndex++;
            Element documentEl = (Element) rootChildNode;
            Node firstFieldNode = getFieldNode(documentEl, 0);
            Node secondFieldNode = getFieldNode(documentEl, 1);
            String firstFieldContent = firstFieldNode.getTextContent();
            String secondFieldContent = secondFieldNode.getTextContent();
            DoubleFieldItem item = getItem(itemIndex, firstFieldContent, secondFieldContent);
            items.add(item);
        }
        return items;
    }

    private static DoubleFieldItem getItem(int itemIndex, String firstFieldContent, String secondFieldContent) {
        DoubleFieldItem item = new DoubleFieldItem(itemIndex);
        item.setFirstFieldContent(firstFieldContent);
        item.setSecondFieldContent(secondFieldContent);
        return item;
    }

    private static Node getFieldNode(Node node, int i) {
        NodeList docChildNodes = node.getChildNodes();
        Node fieldNode = null;
        int foundIndex = -1;
        for (int j = 0; j < docChildNodes.getLength(); j++){
            Node docChildNode = docChildNodes.item(j);
            if (docChildNode instanceof Text) continue;
            fieldNode = docChildNode;
            foundIndex++;
            if (foundIndex == i) break;
        }
        return fieldNode;
    }
}
